﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class FightModel : Entity
    {
        public static FightModel Single;
        public BattlePlayer player1, player2,nowPlayer;
        public BattleStatus BattleStatus;
        public bool TrapUseAble;

        public BattlePlayer GetAttackPlayer()
        {
            return nowPlayer;
        }
        public BattlePlayer GetDefencePlayer()
        {
            if (player1 == nowPlayer) return player2;
            else return player1;
        }

        public async void NextStatus(BattlePlayer battlePlayer)
        {
            TurnInfo turnInfo = new TurnInfo() { BattleStatus = BattleStatus, BattlePlayer = battlePlayer };
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.BattleStatusSwitch, this, turnInfo);
            BattleStatus = turnInfo.BattleStatus;
            BattleView battleView = (BattleView)Game.Scene.GetComponent<FGuiComponent>().GetView(UIViewType.Battle);
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FreshUI, this);
            //battleView.FreshBattle(this);
            Log.Debug($"现在是{nowPlayer.Name}的{BattleStatus.ToString()}阶段");
            
        }

        public async void EndGame_GorgeousCal()
        {
            int p1Source = 0, p2Source = 0;
            GorgeousInfo gorgeousInfo = new GorgeousInfo()
            {
                Cards = player1[CardPlace.SpellChain].Cards,
                Player= player1,
            };
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.GorgeousCal, this, gorgeousInfo);
            foreach (var a in gorgeousInfo.Cards)
            {
                p1Source += a.GetComponent<SpellCardComponent>().Gorgeous;
            }

            gorgeousInfo = new GorgeousInfo()
            {
                Cards = player2[CardPlace.SpellChain].Cards,
                Player = player2,
            };
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.GorgeousCal, this, gorgeousInfo);
            foreach (var a in gorgeousInfo.Cards)
            {
                p2Source += a.GetComponent<SpellCardComponent>().Gorgeous;
            }
            BattlePlayer Winer = p1Source > p2Source ? player1 : player2;
            BattleStatus=BattleStatus.BattleEnd;
            Log.Debug($"{Winer.Name}Win The Game By Gorgeous");
        }

        public void EndGame_Hp()
        {
            BattlePlayer Winer = player1.Hp > 0 ? player1: player2;
            BattleStatus = BattleStatus.BattleEnd;
            Log.Debug($"{Winer.Name}Win The Game By Attack");
        }

        public void EndGame_Sp(BattleEndInfo battleEndInfo)
        {
            BattleStatus = BattleStatus.BattleEnd;
            Log.Debug($"{battleEndInfo.SpecialWinner.Name}Win The Game By Special");
        }


        public void ChangeNowPlayer()
        {
            if (nowPlayer == player1) nowPlayer = player2;
            else nowPlayer = player1;
        }
    }
}
